import { getSubmitOrder, getTradeInfo } from "../../api"

const state = {
    tradeInfo: "", //交易页信息(商品清单)
    orderId: ""  //订单号
}
const actions = {
    /* 获取交易页信息(商品清单) */
    async reqTradeInfo({ commit }) {
        const result = await getTradeInfo();
        if (result.code === 200) {
            commit("RECEIVE_TRADEINFO", result.data)
        } else {
            return Promise.reject("获取交易页信息异常！")
        }
    },
    /* 提交订单 服务器返回一个订单编号*/
    async reqSubmitOrder({ commit }, { tradeNo, tradeInfos }) {
        const result = await getSubmitOrder({ tradeNo, tradeInfos });
        if (result.code === 200) {
            commit("RECEIVE_ORDERID", result.data)
            return result.data
        } else {
            return Promise.reject(new Error(result.message))
        }
    }
}
const mutations = {
    /* 存储交易页信息(商品清单) */
    RECEIVE_TRADEINFO(state, tradeInfo) {
        state.tradeInfo = tradeInfo
    },
    /* 存储 提交订单返回的订单号 */
    RECEIVE_ORDERID(state, orderId) {
        state.orderId = orderId
    }
}
const getters = {}

export default {
    state,
    actions,
    mutations,
    getters
}